home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Snippets / OS Utilities / Sleep Queue Entry / SleepQ.c
Encoding:
C/C++ Source or Header  |  1993-01-19  |  2.2 KB  |  83 lines  |  [TEXT/KAHL]

  1. /* This snippet demonstrates how to create a sleep queue entry under Think C */
  2.  
  3.  
  4. #include "Power.h"
  5. #include "SetUpa4.h"
  6.  
  7. pascal long MySleepQueueEntry(void);
  8.  
  9. SleepQRec gMySleepQStruct;
  10.  
  11. /*
  12.   Here's where we install our sleep queue entry.  This is all
  13.   by the book, as documented in Inside Mac volume VI.
  14. */
  15. void InstallOurSleepQueueEntry()
  16. {
  17.     RememberA4();
  18.     /* install our test routine in the sleep process queue */
  19.     gMySleepQStruct.sleepQLink = 0;
  20.     gMySleepQStruct.sleepQType = slpQType;
  21.     gMySleepQStruct.sleepQProc = (ProcPtr) &MySleepQueueEntry;
  22.     gMySleepQStruct.sleepQFlags = 0;
  23.     
  24.     SleepQInstall(&gMySleepQStruct);
  25.  
  26. }
  27.  
  28. /*    This routine can be called at any of four ways as indicated by D0.  
  29.     When it's called, register A0 contains a pointer to our sleep queue record, 
  30.     and register D0 contains a "what's happening" value:
  31.     1 = sleep request (i.e. "Can I sleep?")
  32.     2 = sleep demand (i.e. "You must sleep.")
  33.     3 = wakeup demand (i.e. "You must wake up.")
  34.     4 = sleep-request revocation (i.e. "I changed my mind about putting you to sleep.")
  35.     
  36.     Since Think C depends upon C calling conventions, you need special in-line
  37.     assembly to handle this routine.
  38. */
  39.  
  40. pascal long MySleepQueueEntry(void)
  41. {
  42.     long        whatToDo;    /* passed to us in D0 */
  43.     SleepQRec    *sqPtr;        /* passed to us in A0 */
  44.     long        returnValue;
  45.  
  46. /* Important: These assembly instructions must be the first executable code
  47. ** in this routine, or you have the potential of changing D0 and/or A0 before
  48. ** you get a chance to save them.  Don't put any code before this asm statement!
  49. */
  50.     asm {
  51.             MOVE.L    D0, whatToDo
  52.             MOVE.L    A0, sqPtr
  53.     }
  54.     
  55.     SetUpA4();
  56.     returnValue = 0;        /* 0 = okay whatever the system wants to do */
  57.  
  58. /* At this point, you'd do whatever you want to do at sleep time.
  59. */
  60.     switch (whatToDo) {
  61.         case sleepRequest:
  62.             DebugStr("\pIn sleepRequest");
  63.             break;
  64.         case sleepDemand:
  65.             DebugStr("\pIn sleepDemand");
  66.             break;
  67.         case sleepWakeUp:
  68.             DebugStr("\pIn sleepWakeUp");
  69.             break;
  70.         case sleepRevoke:
  71.             DebugStr("\pIn sleepRevoke");
  72.             break;
  73.     }
  74.  
  75.     RestoreA4();
  76. /* Important: this asm statement must be the last executable code in this
  77. ** routine, or you won't return the proper value in D0.
  78. */
  79.     asm {
  80.             MOVE.L    returnValue, D0
  81.     }
  82. }
  83.